package com.szl.sbssh.service.impl;

import com.szl.sbssh.jpa.RoleRepository;
import com.szl.sbssh.model.Trole;
import com.szl.sbssh.pageModel.base.PageFilter;
import com.szl.sbssh.pageModel.base.Tree;
import com.szl.sbssh.pageModel.sys.Role;
import com.szl.sbssh.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.*;
import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;

/**
 * Author: songzhonglin
 * Date: 2018/4/25
 * Time: 15:58
 * Description:
 **/
@Service
@Transactional
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleRepository roleRepository;

    @Override
    public List<Role> dataGrid(Role role, PageFilter ph) {
        return null;
    }

    @Override
    public Long count(Role role, PageFilter ph) {
        return null;
    }

    @Override
    public void add(Role role) {

    }

    @Override
    public void delete(Long id) {

    }

    @Override
    public void edit(Role role) {

    }

    @Override
    public Role get(Long id) {
        return null;
    }

    @Override
    public void grant(Role role) {

    }

    @Override
    public List<Tree> tree() {
        List<Tree> lt = new ArrayList<Tree>();
        List<Trole> l = roleRepository.findAll(new Specification<Trole>() {
            @Nullable
            @Override
            public Predicate toPredicate(Root<Trole> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                query.orderBy(cb.asc(root.get("seq").as(Integer.class)));
                return query.getRestriction();
            }
        });
        if ((l != null) && (l.size() > 0)) {
            for (Trole r : l) {
                Tree tree = new Tree();
                tree.setId(r.getId().toString());
                tree.setText(r.getName());
                lt.add(tree);
            }
        }
        return lt;
    }
}
